/*
 * This file is part of "Sorting Thoughts" project.
 *
 * Copyright (c) 2010 Hendrik Ebel
 * All Rights Reserved.
 *
 * Please visit http://code.google.com/p/sorting-thoughts/ for more information.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 * --LICENSE NOTICE--
 */
function LockAssistant(){
	this.password  = "";
}

LockAssistant.prototype.setup = function(){
	Mojo.Log.info("#######################################");
    Mojo.Log.info("Setup lock scene");
    Mojo.Log.info("#######################################");
	// password field
	var attributes = {
		hintText: $L('enter password'),
		textFieldName: 'passwordField',
		modelProperty: 'value',
		label: 'password'
	};
	this.model = {
		'value' : '',
		disable: false
	};
	this.controller.setupWidget('passwordField', attributes, this.model);
	this.passwordChanged = this.passwordChanged.bindAsEventListener(this);
	Mojo.Event.listen(this.controller.get('passwordField'), Mojo.Event.propertyChange, this.passwordChanged);
	
	// action button
	this.save = this.save.bindAsEventListener(this);
	Mojo.Event.listen(this.controller.get('save'), Mojo.Event.tap, this.save);

	// add enter pressed handler
	this.keyupHandler = this.keyupHandler.bindAsEventListener(this);
	this.controller.document.addEventListener('keyup', this.keyupHandler, true);
};

LockAssistant.prototype.activate = function(event) {
	if (SortingThoughts.isTouchPad()) {
		$('lockWrapper').removeClassName("cover");
	    $('lockWrapper').addClassName("coverTouchpad");
		
		$('lockTableWrapper').removeClassName("lockTable");
	    $('lockTableWrapper').addClassName("lockTableTouchpad");
		
		$('lockSpanWrapper').removeClassName("lockSpan");
	    $('lockSpanWrapper').addClassName("lockSpanTouchpad");
		
		if (this.controller.stageController.setWindowOrientation) {
        	this.controller.stageController.setWindowOrientation("right");
		}
    } else {
		if (Mojo.Environment.DeviceInfo.touchableRows < 8) {
	        $('lockWrapper').removeClassName("cover");
	     	$('lockWrapper').addClassName("coverPixi");
	    }
	}
	
	
};

LockAssistant.prototype.deactivate = function(event){
	Mojo.Log.info("LockAssistant deactivate");
};

LockAssistant.prototype.cleanup = function(){
	Mojo.Log.info("LockAssistant clean up");
	Mojo.Event.stopListening(this.controller.get('save'), Mojo.Event.tap, this.save);
	Mojo.Event.stopListening(this.controller.get('passwordField'), Mojo.Event.propertyChange, this.passwordChanged);
	this.controller.document.removeEventListener('keyup', this.keyupHandler, true);
};

LockAssistant.prototype.keyupHandler = function(event) {
  if (Mojo.Char.isEnterKey(event.keyCode)) {
	if(event.srcElement.parentElement.id == "passwordField") {
		Mojo.Log.info("enter pressed");
		$('passwordField').mojo.blur();
		setTimeout(this.save.bind(this), 300);
	}
  }
}

LockAssistant.prototype.passwordChanged = function(event) {
	Mojo.Log.info("password changed");
	this.password = event.value;
};

LockAssistant.prototype.save = function(event) {
	Mojo.Log.info("Ok pressed");
	var comp = hex_md5(SortingThoughts.curLockTwo + SortingThoughts.sicher + this.password + SortingThoughts.istSicher);
	if (comp != SortingThoughts.curLock) {
		$('errorMsg').removeClassName("hiddenError");
     	$('errorMsg').addClassName("showError");
	} else {
		Mojo.Log.info("password is correct");
		Mojo.Controller.stageController.popScene("lock");
		
		// go to overview
		Mojo.Controller.stageController.pushScene({
	        transition: Mojo.Transition.zoom,
	        name: "overview"
	    });
	}
};
